package org.jarx.android.reader;

import java.io.Serializable;
import android.app.Activity; 
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;

public class Query implements Parcelable {

    public static final Parcelable.Creator<Query> CREATOR
            = new Parcelable.Creator<Query>() {

        public Query createFromParcel(Parcel src) {
            return new Query(src);
        }

        public Query[] newArray(int size) {
            return new Query[size];
        }
    };

    private Uri uri;
    private String[] selection;
    private String where;
    private String[] whereArgs;
    private String orderBy;

    public Query(Uri uri, String[] selection, String where, String[] whereArgs,
            String orderBy) {
        this.uri = uri;
        this.selection = selection;
        this.where = where;
        this.whereArgs = whereArgs;
        this.orderBy = orderBy;
    }

    public Query(Parcel src) {
        this.uri = (Uri) src.readParcelable(Query.class.getClassLoader());
        int selectionSize = src.readInt();
        if (selectionSize >= 0) {
            this.selection = new String[selectionSize];
            src.readStringArray(this.selection);
        }
        this.where = src.readString();
        int whereArgsSize = src.readInt();
        if (whereArgsSize > 0) {
            this.whereArgs = new String[whereArgsSize];
            src.readStringArray(this.whereArgs);
        }
        this.orderBy = src.readString();
    }

    public Query() {
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(this.uri, flags);
        dest.writeInt((this.selection == null) ? -1: this.selection.length);
        if (this.selection != null) {
            dest.writeStringArray(this.selection);
        }
        dest.writeString(this.where);
        dest.writeInt((this.whereArgs == null) ? -1: this.whereArgs.length);
        if (this.whereArgs != null) {
            dest.writeStringArray(this.whereArgs);
        }
        dest.writeString(this.orderBy);
    }

    public Uri getUri() {
        return this.uri;
    }

    public void setUri(Uri uri) {
        this.uri = uri;
    }

    public String[] getSelection() {
        return this.selection;
    }

    public void setSelection(String[] selection) {
        this.selection = selection;
    }

    public String getWhere() {
        return this.where;
    }

    public void setWhere(String where) {
        this.where = where;
    }

    public String[] getWhereArgs() {
        return this.whereArgs;
    }

    public void setWhereArgs(String[] whereArgs) {
        this.whereArgs = whereArgs;
    }

    public String getOrderBy() {
        return this.orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public Cursor managedQuery(Activity a) {
        return a.managedQuery(this.uri, this.selection, this.where,
            this.whereArgs, this.orderBy);
    }

    public Cursor query(Activity a) {
        return query(a.getApplicationContext());
    }

    public Cursor query(Context c) {
        ContentResolver cr = c.getContentResolver();
        return cr.query(this.uri, this.selection, this.where, this.whereArgs,
            this.orderBy);
    }
}
